package com.nttdata.ta.todo.controller;

import com.nttdata.ta.common.dto.ResponseResult;
import com.nttdata.ta.common.dto.TodoDTO;
import com.nttdata.ta.todo.service.TodoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/public")
public class PublicTodoController {
    
    private final TodoService todoService;
    
    @GetMapping("/todos")
    public ResponseEntity<ResponseResult<List<TodoDTO>>> getAllTodos() {
        try {
            log.info("公共API - 获取所有待办事项");
            
            // 获取系统中的所有待办事项
            List<TodoDTO> allTodos = todoService.getAllTodosByUserId(null);
            
            // 出于安全考虑，可以过滤掉一些敏感信息
            List<TodoDTO> filteredTodos = allTodos.stream()
                    .map(todo -> new TodoDTO(
                            todo.getId(),
                            todo.getCategory(),
                            todo.getName(),
                            todo.isComplete(),
                            null // 不返回用户ID
                    ))
                    .collect(Collectors.toList());
            
            return ResponseEntity.ok(ResponseResult.success(filteredTodos));
        } catch (Exception e) {
            log.error("获取所有待办事项失败", e);
            return ResponseEntity.badRequest().body(ResponseResult.fail(e.getMessage()));
        }
    }
} 